﻿// ===============================================================================
//  Copyright © ymind.net. All rights reserved .
//  官方网站：http://ymind.net/
//  版权所有：彦铭工作室 老陈出品
// ===============================================================================

using System.Collections;
using System.Linq;
using Lcsoft.UrlRewriter.Conditions;

namespace Lcsoft.UrlRewriter.Actions
{
	/// <summary>
	/// 提供 Url 地址重写功能的地址重写动作。
	/// </summary>
	public sealed class RewriteAction : SetLocationAction, IRewriteCondition
	{
		private readonly RewriteProcessing _processing;

		/// <summary>
		/// 初始化 <see cref="RewriteAction"/> 类的新实例。
		/// </summary>
		/// <param name="location">映射地址。</param>
		/// <param name="processing"><see cref="RewriteProcessing"/> 枚举值之一。</param>
		public RewriteAction(string location, RewriteProcessing processing) : base(location)
		{
			this.Conditions = new ArrayList();
			this._processing = processing;
		}

		/// <summary>
		/// 获取地址重写先决条件的集合。
		/// </summary>
		public ArrayList Conditions { get; private set; }

		#region IRewriteCondition Members

		/// <summary>
		/// 判断条件是否满足。
		/// </summary>
		/// <param name="context"><see cref="RewriteContext"/> 对象的实例。</param>
		/// <returns>如果条件满足则返回 true，否则返回 false。</returns>
		public bool IsMatch(RewriteContext context) { return this.Conditions.Cast<IRewriteCondition>().All(condition => condition.IsMatch(context)); }

		#endregion

		/// <summary>
		/// 执行动作。
		/// </summary>
		/// <param name="context"><see cref="RewriteContext"/> 对象的实例。</param>
		/// <returns>返回 <see cref="RewriteProcessing"/> 枚举值之一。</returns>
		public override RewriteProcessing Execute(RewriteContext context)
		{
			base.Execute(context);

			return this._processing;
		}
	}
}
